<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="仓库名称" prop="wareHouseName">
              <el-input v-model="queryParams.wareHouseName" placeholder="请输入仓库名称" clearable />
            </el-form-item>
            <el-form-item label="品名" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入品名" clearable />
            </el-form-item>
            <el-form-item label="SKU" prop="sku">
              <el-input v-model="queryParams.sku" placeholder="请输入SKU" clearable />
            </el-form-item>
            <el-form-item label="FNSKU" prop="fnsku">
              <el-input v-model="queryParams.fnsku" placeholder="请输入FNSKU" clearable />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <!--          <el-col :span="1.5">-->
          <!--            <el-button v-hasPermi="['lingxing:purchaseOrderItemList:add']" type="primary" plain icon="Plus"-->
          <!--                       @click="handleAdd">新增-->
          <!--            </el-button>-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button-->
          <!--              v-hasPermi="['lingxing:purchaseOrderItemList:edit']"-->
          <!--              type="success"-->
          <!--              plain-->
          <!--              icon="Edit"-->
          <!--              :disabled="single"-->
          <!--              @click="handleUpdate()"-->
          <!--            >修改-->
          <!--            </el-button-->
          <!--            >-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button-->
          <!--              v-hasPermi="['lingxing:purchaseOrderItemList:remove']"-->
          <!--              type="danger"-->
          <!--              plain-->
          <!--              icon="Delete"-->
          <!--              :disabled="multiple"-->
          <!--              @click="handleDelete()"-->
          <!--            >删除-->
          <!--            </el-button-->
          <!--            >-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button v-hasPermi="['lingxing:purchaseOrderItemList:export']" type="warning" plain icon="Download"-->
          <!--                       @click="handleExport"-->
          <!--            >导出-->
          <!--            </el-button-->
          <!--            >-->
          <!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Close" @click="handleClose">关闭</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="purchaseOrderItemListList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="采购单子项id" align="center" prop="id" />
        <el-table-column label="采购单号" align="center" prop="purchaseOrderSn" width="120" />
        <el-table-column label="仓库名称" align="center" prop="wareHouseName" />
        <el-table-column label="品名" align="center" prop="productName" width="120" />
        <el-table-column label="SKU" align="center" prop="sku" width="120" />
        <el-table-column label="FNSKU" align="center" prop="fnsku" />
        <el-table-column label="型号" align="center" prop="model" />
        <el-table-column label="含税单价" align="center" prop="price" />
        <el-table-column label="价税合计" align="center" prop="amount" />
        <el-table-column label="计划采购量" align="center" prop="quantityPlan" />
        <el-table-column label="实际采购量" align="center" prop="quantityReal" />
        <el-table-column label="到货入库量" align="center" prop="quantityEntry" />
        <el-table-column label="待到货量" align="center" prop="quantityReceive" />
        <el-table-column label="退货数" align="center" prop="quantityReturn" />
        <el-table-column label="换货量" align="center" prop="quantityExchange" />
        <el-table-column label="质检量" align="center" prop="quantityQc" />
        <el-table-column label="待质检量" align="center" prop="quantityQcPrepare" />
        <el-table-column label="期待到货时间" align="center" prop="expectArriveTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.expectArriveTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="箱数" align="center" prop="casesNum" />
        <el-table-column label="单箱数量" align="center" prop="quantityPerCase" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <!--          <template #default="scope">-->
          <!--            <el-tooltip content="修改" placement="top">-->
          <!--              <el-button-->
          <!--                v-hasPermi="['lingxing:purchaseOrderItemList:edit']"-->
          <!--                link-->
          <!--                type="primary"-->
          <!--                icon="Edit"-->
          <!--                @click="handleUpdate(scope.row)"-->
          <!--              ></el-button>-->
          <!--            </el-tooltip>-->
          <!--            <el-tooltip content="删除" placement="top">-->
          <!--              <el-button-->
          <!--                v-hasPermi="['lingxing:purchaseOrderItemList:remove']"-->
          <!--                link-->
          <!--                type="primary"-->
          <!--                icon="Delete"-->
          <!--                @click="handleDelete(scope.row)"-->
          <!--              ></el-button>-->
          <!--            </el-tooltip>-->
          <!--          </template>-->
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改领星采购单子项对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="purchaseOrderItemListFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购单子项id" prop="purchaseOrderId">
          <el-input v-model="form.purchaseOrderId" placeholder="请输入采购单子项id" />
        </el-form-item>
        <el-form-item label="仓库id" prop="wid">
          <el-input v-model="form.wid" placeholder="请输入仓库id" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="wareHouseName">
          <el-input v-model="form.wareHouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="采购计划号" prop="planSn">
          <el-input v-model="form.planSn" placeholder="请输入采购计划号" />
        </el-form-item>
        <el-form-item label="本地产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入本地产品id" />
        </el-form-item>
        <el-form-item label="品名" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入品名" />
        </el-form-item>
        <el-form-item label="SKU" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入SKU" />
        </el-form-item>
        <el-form-item label="FNSKU" prop="fnsku">
          <el-input v-model="form.fnsku" placeholder="请输入FNSKU" />
        </el-form-item>
        <el-form-item label="店铺id" prop="sid">
          <el-date-picker v-model="form.sid" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择店铺id">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="含税单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入含税单价" />
        </el-form-item>
        <el-form-item label="价税合计" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入价税合计" />
        </el-form-item>
        <el-form-item label="计划采购量" prop="quantityPlan">
          <el-input v-model="form.quantityPlan" placeholder="请输入计划采购量" />
        </el-form-item>
        <el-form-item label="实际采购量" prop="quantityReal">
          <el-input v-model="form.quantityReal" placeholder="请输入实际采购量" />
        </el-form-item>
        <el-form-item label="到货入库量" prop="quantityEntry">
          <el-input v-model="form.quantityEntry" placeholder="请输入到货入库量" />
        </el-form-item>
        <el-form-item label="待到货量" prop="quantityReceive">
          <el-input v-model="form.quantityReceive" placeholder="请输入待到货量" />
        </el-form-item>
        <el-form-item label="退货数" prop="quantityReturn">
          <el-input v-model="form.quantityReturn" placeholder="请输入退货数" />
        </el-form-item>
        <el-form-item label="换货量" prop="quantityExchange">
          <el-input v-model="form.quantityExchange" placeholder="请输入换货量" />
        </el-form-item>
        <el-form-item label="质检量" prop="quantityQc">
          <el-input v-model="form.quantityQc" placeholder="请输入质检量" />
        </el-form-item>
        <el-form-item label="待质检量" prop="quantityQcPrepare">
          <el-input v-model="form.quantityQcPrepare" placeholder="请输入待质检量" />
        </el-form-item>
        <el-form-item label="期待到货时间" prop="expectArriveTime">
          <el-date-picker
            v-model="form.expectArriveTime"
            clearable
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择期待到货时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="箱数" prop="casesNum">
          <el-input v-model="form.casesNum" placeholder="请输入箱数" />
        </el-form-item>
        <el-form-item label="单箱数量" prop="quantityPerCase">
          <el-input v-model="form.quantityPerCase" placeholder="请输入单箱数量" />
        </el-form-item>
        <el-form-item label="是否删除：0 否，1 是" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除：0 否，1 是" />
        </el-form-item>
        <el-form-item label="MSKU" prop="msku">
          <el-input v-model="form.msku" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="属性" prop="attribute">
          <el-input v-model="form.attribute" placeholder="请输入属性" />
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="form.taxRate" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="spu" prop="spu">
          <el-input v-model="form.spu" placeholder="请输入spu" />
        </el-form-item>
        <el-form-item label="款名" prop="spuName">
          <el-input v-model="form.spuName" placeholder="请输入款名" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PurchaseOrderItemList" lang="ts">
import {
  listPurchaseOrderItemList,
  getPurchaseOrderItemList,
  delPurchaseOrderItemList,
  addPurchaseOrderItemList,
  updatePurchaseOrderItemList
} from '@/api/lingxing/purchaseOrderItemList';
import { PurchaseOrderItemListVO, PurchaseOrderItemListQuery, PurchaseOrderItemListForm } from '@/api/lingxing/purchaseOrderItemList/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
import { RouteLocationNormalized } from 'vue-router';

const route = useRoute();

const purchaseOrderItemListList = ref<PurchaseOrderItemListVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const purchaseOrderItemListFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: PurchaseOrderItemListForm = {
  id: undefined,
  purchaseOrderId: undefined,
  wid: undefined,
  wareHouseName: undefined,
  planSn: undefined,
  productId: undefined,
  productName: undefined,
  sku: undefined,
  fnsku: undefined,
  sid: undefined,
  model: undefined,
  price: undefined,
  amount: undefined,
  quantityPlan: undefined,
  quantityReal: undefined,
  quantityEntry: undefined,
  quantityReceive: undefined,
  quantityReturn: undefined,
  quantityExchange: undefined,
  quantityQc: undefined,
  quantityQcPrepare: undefined,
  expectArriveTime: undefined,
  remark: undefined,
  casesNum: undefined,
  quantityPerCase: undefined,
  isDelete: undefined,
  msku: undefined,
  attribute: undefined,
  taxRate: undefined,
  spu: undefined,
  spuName: undefined
};
const data = reactive<PageData<PurchaseOrderItemListForm, PurchaseOrderItemListQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    purchaseOrderId: undefined,
    purchaseOrderSn: undefined,
    wid: undefined,
    wareHouseName: undefined,
    planSn: undefined,
    productId: undefined,
    productName: undefined,
    sku: undefined,
    fnsku: undefined,
    sid: undefined,
    model: undefined,
    price: undefined,
    amount: undefined,
    quantityPlan: undefined,
    quantityReal: undefined,
    quantityEntry: undefined,
    quantityReceive: undefined,
    quantityReturn: undefined,
    quantityExchange: undefined,
    quantityQc: undefined,
    quantityQcPrepare: undefined,
    expectArriveTime: undefined,
    casesNum: undefined,
    quantityPerCase: undefined,
    isDelete: undefined,
    msku: undefined,
    attribute: undefined,
    taxRate: undefined,
    spu: undefined,
    spuName: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '采购单子项id不能为空', trigger: 'blur' }],
    purchaseOrderId: [{ required: true, message: '采购单子项id不能为空', trigger: 'blur' }],
    wid: [{ required: true, message: '仓库id不能为空', trigger: 'blur' }],
    wareHouseName: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }],
    planSn: [{ required: true, message: '采购计划号不能为空', trigger: 'blur' }],
    productId: [{ required: true, message: '本地产品id不能为空', trigger: 'blur' }],
    productName: [{ required: true, message: '品名不能为空', trigger: 'blur' }],
    sku: [{ required: true, message: 'SKU不能为空', trigger: 'blur' }],
    fnsku: [{ required: true, message: 'FNSKU不能为空', trigger: 'blur' }],
    sid: [{ required: true, message: '店铺id不能为空', trigger: 'blur' }],
    model: [{ required: true, message: '型号不能为空', trigger: 'blur' }],
    price: [{ required: true, message: '含税单价不能为空', trigger: 'blur' }],
    amount: [{ required: true, message: '价税合计不能为空', trigger: 'blur' }],
    quantityPlan: [{ required: true, message: '计划采购量不能为空', trigger: 'blur' }],
    quantityReal: [{ required: true, message: '实际采购量不能为空', trigger: 'blur' }],
    quantityEntry: [{ required: true, message: '到货入库量不能为空', trigger: 'blur' }],
    quantityReceive: [{ required: true, message: '待到货量不能为空', trigger: 'blur' }],
    quantityReturn: [{ required: true, message: '退货数不能为空', trigger: 'blur' }],
    quantityExchange: [{ required: true, message: '换货量不能为空', trigger: 'blur' }],
    quantityQc: [{ required: true, message: '质检量不能为空', trigger: 'blur' }],
    quantityQcPrepare: [{ required: true, message: '待质检量不能为空', trigger: 'blur' }],
    expectArriveTime: [{ required: true, message: '期待到货时间不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
    casesNum: [{ required: true, message: '箱数不能为空', trigger: 'blur' }],
    quantityPerCase: [{ required: true, message: '单箱数量不能为空', trigger: 'blur' }],
    isDelete: [{ required: true, message: '是否删除：0 否，1 是不能为空', trigger: 'blur' }],
    msku: [{ required: true, message: 'MSKU不能为空', trigger: 'blur' }],
    attribute: [{ required: true, message: '属性不能为空', trigger: 'blur' }],
    taxRate: [{ required: true, message: '税率不能为空', trigger: 'blur' }],
    spu: [{ required: true, message: 'spu不能为空', trigger: 'blur' }],
    spuName: [{ required: true, message: '款名不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询领星采购单子项列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.purchaseOrderSn = route.params.purchaseOrderSn as string; // 增加了这一句固定的参数
  const res = await listPurchaseOrderItemList(queryParams.value);
  purchaseOrderItemListList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  purchaseOrderItemListFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: PurchaseOrderItemListVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加领星采购单子项';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: PurchaseOrderItemListVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getPurchaseOrderItemList(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改领星采购单子项';
};

/** 提交按钮 */
const submitForm = () => {
  purchaseOrderItemListFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updatePurchaseOrderItemList(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addPurchaseOrderItemList(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: PurchaseOrderItemListVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除领星采购单子项编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delPurchaseOrderItemList(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'lingxing/purchaseOrderItemList/export',
    {
      ...queryParams.value
    },
    `purchaseOrderItemList_${new Date().getTime()}.xlsx`
  );
};

/** 返回按钮操作 */
const handleClose = () => {
  const obj: RouteLocationNormalized = {
    fullPath: '',
    hash: '',
    matched: [],
    meta: undefined,
    name: undefined,
    params: undefined,
    query: undefined,
    redirectedFrom: undefined,
    path: '/purchase/purchase/purchaseOrder'
  };
  proxy?.$tab.closeOpenPage(obj);
};

onMounted(() => {
  getList();
});
</script>
